﻿using FactoryClsssLib.Examples;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryClsssLib.Factory
{
    public class ShapeFactory : IAbstractFactory<IShape>
    {
        public Dictionary<string, Type> keyValuePairs
        {
            get
            {
                return new Dictionary<string, Type>()
                {
                    { "Circle" , typeof(Circle)},
                    {"Rectangle" ,typeof(Rectangle) },
                    {"Square",typeof(Square) }
                };
            }
        }

        public override IShape getObject(string key)
        {
            if (key == null)
            {
                return null;
            }
            if (key.Equals("CIRCLE"))
            {
                return new Circle();
            }
            else if (key.Equals("RECTANGLE"))
            {
                return new Rectangle();
            }
            else if (key.Equals("SQUARE"))
            {
                return new Square();
            }
            return null;
        }

        /// <summary>
        ///  使用配置的方式
        ///   1、json/数据库
        ///   2、反射
        ///   3、字典
        /// </summary>
        /// <param name="shape"></param>
        /// <returns></returns>
        public IShape Getreflect<T>() where T : class, IShape, new()
        {
            Type shape = typeof(T);
            if (shape.GetInterface("IShape") == null || keyValuePairs[shape.Name] == null)
                return null;
            else
            {
                Type type = keyValuePairs[shape.Name];
                T data = (T)Activator.CreateInstance(type);
                return data;
            }
        }

        /// <summary>
        ///  使用配置的方式
        ///   1、json/数据库
        ///   2、反射
        ///   3、字典
        /// </summary>
        /// <param name="shape"></param>
        /// <returns></returns>
        //public IShape getShape(string shape)
        //{

        //    if (shape == null)
        //    {
        //        return null;
        //    }
        //    if (shape.Equals("CIRCLE"))
        //    {
        //        return new Circle();
        //    }
        //    else if (shape.Equals("RECTANGLE"))
        //    {
        //        return new Rectangle();
        //    }
        //    else if (shape.Equals("SQUARE"))
        //    {
        //        return new Square();
        //    }
        //    return null;
        //}
    }
}
